package com.hcb.tonguetiphealth.widget.more;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.hcb.tonguetiphealth.adapter.ContactListAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.model.Contact;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.BottomWidget;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class ContactsWidget extends GeneralWidget implements OnClickListener {

	private Activity ctx;

	private LinearLayout llBack;
	private Handler mHandler;
	private boolean commitSuccess = false;
	private List<Contact> contactList;
	private ListView contactListView;

	private static final String TAG = "ContactWidget";

	public ContactsWidget(Context context) {
		super(context);
		ctx = (Activity) context;
		inflate(context, R.layout.more_share_contact, this);

		new BottomWidget(context, this);
		bindView();
		bindEvent();
		try {
			GetContact();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void bindView() {
		llBack = (LinearLayout) findViewById(R.id.contact_back);
		contactListView = (ListView) findViewById(R.id.contact_listview);
	}

	private void bindEvent() {
 
		contactListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						Contact contact = (Contact)arg0.getAdapter().getItem(arg2);
						String msgBody = getResources().getString(R.string.contact_msm_body);
						msgBody += Global.user.getUserName();
						sendSMS(contact.getPhoneNum(), msgBody);
					}

				});
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MessageWhat.SHOW_DIALOG:
					showMessageDialog(ctx, "正在获取朋友信息...");
					break;
				case MessageWhat.DIMISS_DIALOG:
					ContactListAdapter adapter = new ContactListAdapter(ctx, contactList);
					contactListView.setAdapter(adapter);
					dismissMessageDialog();
					break;
				default:
					break;
				}
			}
		};
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.more_advice_back:
			ctx.setContentView(new MoreWidget(ctx));
			break;
		case R.id.contact_back:
			ctx.setContentView(new MoreWidget(ctx));
			break;
		default:
			break;
		}
	}

	/** 
	 
	* 发送短信 
	 
	* @param smsBody 
	 
	*/  
	  
	private void sendSMS(String tel, String smsBody) {
		Uri smsToUri = Uri.parse("smsto:" + tel);
		Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
		intent.putExtra("sms_body", smsBody);
		ctx.startActivity(intent);
	}
	
	// 查询所有联系人的姓名，电话，邮箱
	public void GetContact() throws Exception {
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		contactList = new ArrayList<Contact>();
		new Thread(){
			public void run() {
				Uri uri = Uri.parse("content://com.android.contacts/contacts");
				ContentResolver resolver = getContext().getContentResolver();
				Cursor cursor = resolver.query(uri, new String[] { "_id" }, null, null,
						null);
				while (cursor.moveToNext()) {
					Contact contact = new Contact();
					int contractID = cursor.getInt(0);
					uri = Uri.parse("content://com.android.contacts/contacts/"
							+ contractID + "/data");
					Cursor cursor1 = resolver.query(uri, new String[] { "mimetype",
							"data1", "data2" }, null, null, null);
					while (cursor1.moveToNext()) {
						String data1 = cursor1.getString(cursor1
								.getColumnIndex("data1"));
						String mimeType = cursor1.getString(cursor1
								.getColumnIndex("mimetype"));
						if ("vnd.android.cursor.item/name".equals(mimeType)) { // 是姓名
							contact.setUserName(data1);
						} else if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { // 手机
							contact.setPhoneNum(data1);
						}
					}
					contactList.add(contact);
					cursor1.close();
				}
				cursor.close();
				mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);

			}
		}.start();
	}

	@Override
	public void notifyAdapter() {

	}
}
